package com.houxiurong.utils;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.net.InetAddress;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * MapValue工具
 * company:houxiurong.com
 * author: 0184/houxiurong
 * date:2016/6/2.
 */
public class MapValueUtil {

    public static String getString(Map<String, ? extends Object> map, String key) {
        Object obj = getObject(map, key);
        if (null == obj) {
            return "";
        }

        if (obj instanceof String) {
            return obj.toString();
        }

        return "";
    }

    public static Integer getInteger(Map<String, ? extends Object> map, String key) {
        Object obj = getObject(map, key);
        if (null == obj) {
            return null;
        }

        if (obj instanceof Integer) {
            return (Integer) obj;
        }

        if (obj instanceof String) {
            return Integer.valueOf(obj.toString());
        }

        return null;
    }

    public static Date getDate(Map<String, ? extends Object> map, String key) {
        Object obj = getObject(map, key);
        if (null == obj) {
            return null;
        }

        if (obj instanceof Date) {
            return (Date) obj;
        }

        return null;
    }

    public static Float getFloat(Map<String, ? extends Object> map, String key) {
        Object obj = getObject(map, key);
        if (null == obj) {
            return null;
        }

        if (obj instanceof Float) {
            return (Float) obj;
        }

        return null;
    }

    public static Double getDouble(Map<String, ? extends Object> map, String key) {
        Object obj = getObject(map, key);
        if (null == obj) {
            return null;
        }

        if (obj instanceof Double) {
            return (Double) obj;
        }

        if (obj instanceof Float) {
            return Double.parseDouble(obj.toString());
        }

        if (obj instanceof Integer) {
            return Double.parseDouble(obj.toString());
        }

        return null;
    }

    public static BigDecimal getBigDecimal(Map<String, ? extends Object> map, String key) {
        Object obj = getObject(map, key);
        if (null == obj) {
            return null;
        }
        if (obj instanceof BigDecimal) {
            return (BigDecimal) obj;
        }

        if(obj instanceof Double){
            return new BigDecimal(obj.toString());
        }

        if(obj instanceof Float){
            return new BigDecimal(obj.toString());
        }

        if(obj instanceof Integer){
            return new BigDecimal(obj.toString());
        }

        return null;
    }

    public static List getList(Map<String, Object> map, String key){
        Object obj = getObject(map, key);
        if (null == obj) {
            return null;
        }

        if (obj instanceof List) {
            return (List) obj;
        }

        return null;
    }

    public static Object getObject(Map<String, ? extends Object> map, String key) {
        if (null == map || StringUtils.isEmpty(key)) {
            return null;
        }

        return map.get(key);
    }

    /**
     * json 转换为 实体对象
     *
     * @param str
     * @param type
     * @param <T>
     * @return
     */
    public static <T> T fromJson(String str, Class<T> type) {
        try {
            T t = JSONUtils.parse(str, type);
            return t;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
